<template>
	<el-form ref="dialogForm" label-width="100px">
		<el-row
			><el-form-item label-width="120px" label="发送(指定)页面"
				><el-select
					placeholder="---请选择请求的方法---"
					v-model="requestIndex"
					style="width: 300px"
					@change="optionChange"
				>
					<el-option
						v-for="(item, i) in optionsInfo"
						:key="item.index"
						:label="`${i + 1}.${item.indexText}`"
						:value="item.index"
					/> </el-select></el-form-item></el-row
		><el-row
			><el-form-item label-width="120px" label="发送内容"
				><el-input
					v-model="requestRemark"
					placeholder="发送内容"
					style="width: 1200px"
					rows="10"
					type="textarea" /></el-form-item
		></el-row>
		<el-row
			><el-form-item label-width="120px" label=""
				><el-button @click="submit">提交</el-button></el-form-item
			></el-row
		>
		<el-row
			><el-form-item label-width="120px" label="接收内容"
				><el-input
					v-model="requestResult"
					placeholder="接收内容"
					style="width: 1200px"
					rows="10"
					type="textarea" /></el-form-item></el-row
		><el-row>
			<el-form-item label-width="120px" label="进度"
				><el-progress
				:text-inside="true"
				:stroke-width="24"
				:percentage="percentage"
				status="success"
			></el-progress></el-form-item></el-row
	></el-form>
</template>
<script>
import bus from "@/utils/mitt";
import publicjs from "@/api/model/publicjs";
export default {
	name: "waiterService",
	data() {
		return {
			//表单数据
			dialogVisble: false,
			token: "",
			hasToken: false,
			requestIndex: "websocketTest",
			msgReceivePage: "",
			msgType: "",
			msgContent: "",
			requestResult: "",
			requestRemark: "",
			percentage: 0,
			optionsInfo: [
				{
					index: "websocketTest",
					indexText: "这是个测试而已(本页面)",
					msgReceivePage: "websocketService",
					msgType: "websocket",
					msgContent: {
						data: "这是个测试而已",
						user: "admin",
						currentTime: this.getCurrentTime(),
					},
				},
				{
					index: "orgListRefresh",
					indexText: "门店刷新(模拟,门店列表页面)",
					msgReceivePage: "orgList",
					msgType: "RefreshTime",
					msgContent: {
						Code: "0004",
						LastTime: this.getCurrentTime(),
					},
				},
				{
					index: "dataProcess",
					indexText: "数据处理过程(模拟)",
					msgReceivePage: "dataProcessTest",
					msgType: "dataProcess",
					msgContent: "",
				},
				{
					index: "ClearConnect",
					indexText: "清除所有连接",
					msgReceivePage: "websocketService",
					msgType: "ClearConnect",
					msgContent: "",
				},
				{
					index: "SendMsgToUser",
					indexText: "发送给用户",
					msgReceivePage: "websocketService",
					msgType: "SendMsgToUser",
					msgContent: {
						userCodes:"admin",
						msg:"同志们"

					},
				},
			],
		};
	},
	methods: {
		optionChange() {
			// eslint-disable-next-line no-debugger
			let option = this.optionsInfo.find(
				(item) => item.index == this.requestIndex
			);
			this.requestRemark = JSON.stringify(option);
			this.msgReceivePage = option.msgReceivePage;
			this.msgType = option.msgType;
			this.msgContent = option.msgContent;
		},
		async submit() {
			// bus.emit(this.requestIndex, {
			// 	msgReceivePage: this.msgReceivePage,
			// 	msgType: this.msgType,
			// 	msgContent: this.msgContent,
			// });
			this.percentage = 0;
			var head = new publicjs.head();
			publicjs.initHead(head, "websocket");
			var ed = new publicjs.entitydata(head);
			var eo = `{"receiveType":"${this.requestIndex}","msgReceivePage":"${
				this.msgReceivePage
			}","msgType":"${this.msgType}","msgContent":${JSON.stringify(
				this.msgContent
			)}}`;
			ed.Data.Content.ObjectList.Object.push(eo);
			try {
				var returnValueStr = await this.$API.auth.objProcess.post(ed);
				//alert(returnValueStr);
				var returnValue = JSON.parse(returnValueStr);
				console.log(returnValue);
				// bus.emit(returnValue.receiveType, {
				// 	msgReceivePage: returnValue.msgReceivePage,
				// 	msgType: returnValue.msgType,
				// 	msgContent: returnValue.msgContent,
				// });
			} catch (error) {
				this.$alert(error.message, "提示", { type: "error" });
				return false;
			}
		},
		getCurrentTime() {
			let nowDate = new Date();
			let date = {
				year: nowDate.getFullYear(),
				month: nowDate.getMonth() + 1,
				date: nowDate.getDate(),
				hours: nowDate.getHours(),
				minutes: nowDate.getMinutes(),
				seconds: nowDate.getSeconds(),
			};
			let newmonth = date.month > 10 ? date.month : "0" + date.month;
			let newday = date.date > 10 ? date.date : "0" + date.date;
			let newminutes =
				date.minutes < 10 ? "0" + date.minutes : date.minutes;
			let newseconds =
				date.seconds < 10 ? "0" + date.seconds : date.seconds;
			// 我需要的时间格式为 "yyyy-MM-dd HH:mm:ss" 有不一样的自己拼别的，都是字符串拼接
			return (
				date.year +
				"-" +
				newmonth +
				"-" +
				newday +
				" " +
				date.hours +
				":" +
				newminutes +
				":" +
				newseconds
			);
		},
	},
	async mounted() {
		this.optionChange();
		await bus.on("websocketService", (e) => {
			this.requestResult = JSON.stringify(e);
			// if (e.msgType == "dataProcess")
			// 	this.percentage = parseInt(e.msgContent.currentIndex);
			this.requestResult =
				'{"msgReceivePage":"' +
				e.msgReceivePage +
				'","msgType":"' +
				e.msgType +
				'","msgContent":' +
				JSON.stringify(e.msgContent) +
				'}';
		});
	},
};
</script>
<style scoped>
.el-progress--line {
	margin-bottom: 15px;
	width: 650px;
	padding: 10px;
}
</style>
